package com.ljc.cjgl.service;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import com.ljc.cjgl.custom.FileUtil;
import com.ljc.cjgl.custom.TableResult;
import com.ljc.cjgl.entitiy.ActivityAttachment;
import com.ljc.cjgl.entitiy.ClassActitvity;
import com.ljc.cjgl.form.ActivityAttachmentForm;
import com.ljc.cjgl.repository.ActivityAttachmentRepository;
import com.ljc.cjgl.security.SysUser;
import com.ljc.cjgl.security.UserUtils;

@Service
public class ActivityAttachmentService {
	@Autowired
	private ActivityAttachmentRepository attachmentDAO;
	@Autowired
	private UserUtils userUtils;
	
	public ActivityAttachment findById(Integer id) {
		Optional<ActivityAttachment> optional = attachmentDAO.findById(id);
		if(optional.isPresent()) {
			return optional.get();
		}
		return null;
	}

	public Page<ActivityAttachment> findAll(Specification<ActivityAttachment> specification,
			PageRequest pageRequest) {
		return attachmentDAO.findAll(specification, pageRequest);
	}
	
	public List<ActivityAttachment> findAll(){
		return attachmentDAO.findAll();
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public synchronized HashMap<String, Object> save(ActivityAttachmentForm form, HttpServletRequest request, MultipartFile file) {
		try {
			SysUser user=userUtils.getUser();
			if(user==null) {
				return TableResult.bulidResult(false, "你不是一个合法用户，不能上传文件");
			}
			ActivityAttachment model=new ActivityAttachment();
			Integer id = form.getId();
			if(id!=null) {
				model=findById(id);
			}
			Integer activityId = form.getActivityId();
			model.setCreator(user.getName());
			if(activityId!=null) {
				model.setActivity(new ClassActitvity(activityId));
			}
			String fileName=file.getOriginalFilename();
			if(fileName==null&&model.getFileName()==null) {
				return TableResult.bulidResult(false, "新增的附件文件不能为空");
			}
			if(StringUtils.hasText(fileName)) {
				String uid=model.getUid();
				if(!StringUtils.hasText(uid)) {
					uid=UUID.randomUUID().toString();
					model.setUid(uid);
				}
				model.setUid(uid);
				fileName=(new File(fileName)).getName();
				model.setFileName(fileName);
				FileUtil.uploadFile(file.getBytes(), uid);
			}
			model.setTitle(form.getTitle());
			model.setDemo(form.getDemo());
			model.setType(form.getType());
			attachmentDAO.save(model);
			return TableResult.bulidResult(true, "数据保存成功");
		} catch (Exception e) {
			return TableResult.bulidResult(false,"文件上传失败");
		}
	}

	public synchronized boolean delete(List<ActivityAttachment> attachments) {
		try {
			for (ActivityAttachment attachment : attachments) {
				attachmentDAO.deleteById(attachment.getId());
			}
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public ActivityAttachment findByUid(String uid) {
		List<ActivityAttachment> list=attachmentDAO.findByUid(uid);
		if(list.isEmpty()) {
			return null;
		}else {
			return list.get(0);
		}
	}

	public List<ActivityAttachment> findByActivityIdAndType(Integer id, int i) {
		return attachmentDAO.findByActivityIdAndType(id,i);
	}


}
